package org.belkodevelop.userdatails.service;

import org.belkodevelop.userdatails.adapter.UserDetailsAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service("userDetailsService")
@Transactional
public class UserDetailsImpl implements UserDetailsService
{
    @Autowired
    private org.belkodevelop.user.dao.UserDAO userDao;

    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException
    {
        User user;
        org.belkodevelop.user.domain.User person = userDao.getUser(userName);
        if (person == null) {
            throw new UsernameNotFoundException("user not found");
        } 
        else {
            user = new UserDetailsAdapter(person);
        }
        return user;
    }
    
}